package com.license.logic.behaviorlog;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.license.logic.alimentation.Food;
import com.license.logic.history.GeneticalHistory;
import com.license.logic.history.MedicalHistory;
import com.license.logic.preference.Activity;
import com.license.logic.preference.Preferences;
import com.license.logic.profile.EmployeeProfile;
import com.license.logic.profile.MedicalProfile;
import com.license.logic.profile.PersonalProfile;

public class FileHelper {
	public void deleteFile(String filePath) {
		File file = new File(filePath);
		if (file.delete()) {
			System.out.println(file.getName() + " is deleted!");
		} else {
			System.out.println("Delete operation is failed.");
		}
	}

	public EmployeeProfile getEmployeeProfile(String filePath)
			throws ParserConfigurationException, SAXException, IOException {
		EmployeeProfile employeeProfile = new EmployeeProfile();

		File fXmlFile = new File(filePath);
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();

		employeeProfile.setPersonalProfile(getPersonalProfile(doc));
		employeeProfile.setMedicalProfile(getMedicalProfile(doc));

		return employeeProfile;
	}

	private PersonalProfile getPersonalProfile(Document doc) {
		PersonalProfile personalProfile = new PersonalProfile();
		personalProfile.setAge(Integer.parseInt(((Element) doc
				.getElementsByTagName("age").item(0)).getTextContent()));
		personalProfile.setName(((Element) doc.getElementsByTagName("name")
				.item(0)).getTextContent());
		personalProfile.setSex(((Element) doc.getElementsByTagName("sex").item(
				0)).getTextContent());
		// personalProfile.setBirthDate(Date.valueOf(((Element)doc.getElementsByTagName("birthdate").item(0)).getTextContent()));

		personalProfile.setHeight(Double.parseDouble(((Element) doc
				.getElementsByTagName("height").item(0)).getTextContent()));
		personalProfile.setWeight(Double.parseDouble(((Element) doc
				.getElementsByTagName("weight").item(0)).getTextContent()));

		// set food preferencies
		NodeList nodeList = doc.getElementsByTagName("preferences").item(0)
				.getChildNodes();
		List<Food> foods = new ArrayList<Food>();

		Node preferencedFoodNodes = nodeList.item(1);
		for (int i = 1; i < preferencedFoodNodes.getChildNodes().getLength(); i += 2) {
			Food food = new Food();
			food.setName(preferencedFoodNodes.getChildNodes().item(i)
					.getTextContent());
			foods.add(food);
		}

		// set activity preferencies
		List<Activity> activities = new ArrayList<Activity>();

		Node preferencedActivityNodes = nodeList.item(3);
		for (int i = 1; i < preferencedActivityNodes.getChildNodes()
				.getLength(); i += 2) {
			Activity activity = new Activity();
			activity.setName(preferencedActivityNodes.getChildNodes().item(i)
					.getTextContent());
			activities.add(activity);
		}

		Preferences preference = new Preferences();
		preference.setNutritions(foods);
		preference.setActivities(activities);

		personalProfile.setPreference(preference);

		return personalProfile;
	}

	private MedicalProfile getMedicalProfile(Document doc) {
		MedicalProfile medicalProfile = new MedicalProfile();

		// set medical profile
		NodeList nodeList = doc.getElementsByTagName("medicalProfile").item(0)
				.getChildNodes();

		// set medical history
		Node medicalhistoryNode = nodeList.item(1);
		MedicalHistory medicalHistory = new MedicalHistory();
		medicalHistory.setSmoker("true".equals(medicalhistoryNode
				.getChildNodes().item(1).getTextContent()));
		medicalHistory.setSbp(Integer.parseInt(medicalhistoryNode
				.getChildNodes().item(3).getTextContent()));
		medicalHistory.setDiabets("true".equals(medicalhistoryNode
				.getChildNodes().item(5).getTextContent()));
		medicalHistory.setActivityKalory(Double.parseDouble(medicalhistoryNode
				.getChildNodes().item(7).getTextContent()));
		medicalHistory.setNutritionKalory(Double.parseDouble(medicalhistoryNode
				.getChildNodes().item(9).getTextContent()));

		// set genetic History
		Node geneticHistory = nodeList.item(3);
		GeneticalHistory geneticalHistory = new GeneticalHistory();
		geneticalHistory.setFamilyCardioHistory("true".equals(geneticHistory
				.getChildNodes().item(1).getTextContent()));

		medicalProfile.setGeneticalHistory(geneticalHistory);
		medicalProfile.setMedicalHistory(medicalHistory);
		return medicalProfile;
	}

	public void copyXMLFile(String sourceLocation, String destLocation)
			throws IOException {

		BufferedReader reader = new BufferedReader(new FileReader(
				sourceLocation));
		BufferedWriter writer = new BufferedWriter(new FileWriter(destLocation));

		String line = null;

		while ((line = reader.readLine()) != null) {
			writer.write(line);
		}

		// Close to unlock.
		reader.close();
		// Close to unlock and flush to disk.
		writer.close();
	}

	public Map<String, Double> getConfigParameters(String path)
			throws ParserConfigurationException, SAXException, IOException {
		Map<String, Double> config = new HashMap<String, Double>();
		File fXmlFile = new File(path);
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();

		boolean isAutomated = ((Element) doc.getElementsByTagName(
				"is_automated").item(0)).getTextContent().equalsIgnoreCase(
				"true") ? true : false;
		if (isAutomated) {
			config.put(
					"paretoOptimalMethod",
					Double.parseDouble(((Element) doc.getElementsByTagName(
							"a_paretoOptimalMethod").item(0)).getTextContent()));
			config.put(
					"particleListSize",
					Double.parseDouble(((Element) doc.getElementsByTagName(
							"a_particleListSize").item(0)).getTextContent()));
			config.put(
					"particleListSize",
					Double.parseDouble(((Element) doc.getElementsByTagName(
							"a_particleListSize").item(0)).getTextContent()));
			config.put(
					"iterationNumber",
					Double.parseDouble(((Element) doc.getElementsByTagName(
							"a_iterationNumber").item(0)).getTextContent()));
			config.put(
					"c1",
					Double.parseDouble(((Element) doc.getElementsByTagName(
							"a_c1").item(0)).getTextContent()));
			config.put(
					"c2",
					Double.parseDouble(((Element) doc.getElementsByTagName(
							"a_c2").item(0)).getTextContent()));
			config.put(
					"c3",
					Double.parseDouble(((Element) doc.getElementsByTagName(
							"a_c3").item(0)).getTextContent()));
			System.out.println("sunt aici" + config.get("iterationNumber"));
		} else {
			config.put("c1", Double.parseDouble(((Element) doc
					.getElementsByTagName("c1").item(0)).getTextContent()));
			config.put("c2", Double.parseDouble(((Element) doc
					.getElementsByTagName("c2").item(0)).getTextContent()));
			config.put("c3", Double.parseDouble(((Element) doc
					.getElementsByTagName("c3").item(0)).getTextContent()));

		}

		return config;
	}
}
